package com.woniu.service.impl;

import com.alibaba.fastjson.JSON;
import com.woniu.client.OrderClient;
import com.woniu.client.ProductionClient;
import com.woniu.outlet.dao.OrderDetailsPoMapper;
import com.woniu.outlet.dao.ProPlanPoMapper;
import com.woniu.outlet.dao.ProProgressPoMapper;
import com.woniu.outlet.po.*;
import com.woniu.service.ProPlanPoService;
import com.woniu.uitl.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

@Service
public class ProPlanPoServiceImpl implements ProPlanPoService {

    @Autowired
    private ProPlanPoMapper proPlanPoMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ProductionClient productionClient;
    @Autowired
    private OrderDetailsPoMapper orderDetailsPoMapper;

    /**
     * 根据订单id查询生产计划单
     * 查询所有生产计划单
     * @param orderId
     * @return
     */
    public List<ProPlanPo> findAllProPlan(Long orderId,String status) {
        ProPlanPoExample proPlanPoExample = new ProPlanPoExample();
        ProPlanPoExample.Criteria criteria = proPlanPoExample.createCriteria();
        if (!StringUtils.isEmpty(orderId)){
            criteria.andOrderIdEqualTo(orderId);
        }
        if (!StringUtils.isEmpty(status)){
            criteria.andStatusEqualTo(status);
        }
        List<ProPlanPo> poList = proPlanPoMapper.selectByExample(proPlanPoExample);
        return poList;
    }

    /**
     *根据生产计划单id查询生产计划详情
     * @param proId
     * @return
     */
    public HashMap<String, Object> getProDetailById(Long proId) {

        HashMap<String,Object> map = new HashMap<>();
        ProPlanPo proPlanPo2 = proPlanPoMapper.selectByPrimaryKey(proId);
        if (StringUtils.isEmpty(proPlanPo2)){
            map.put("msg","无效的生产计划单id");
            return map;
        }
        //查询生产计划单信息
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        String proPlan = ops.get(proId.toString());
        ProPlanPo proPlanPo = null;
        if (!StringUtils.isEmpty(proPlan)){
            proPlanPo = JSON.parseObject(proPlan, ProPlanPo.class);
            map.put("生产计划单信息",proPlanPo);
        }else {
            proPlanPo = proPlanPoMapper.selectByPrimaryKey(proId);
            map.put("生产计划单信息",proPlanPo);
        }
        //产品目录信息
        List<OrderDetailsPo> poList = orderDetailsPoMapper.findAllProListByOrderId(proPlanPo.getId());
        if (poList.size() != 0){
            map.put("产品目录信息",poList);
        }
        //生产进度信息
        ResponseResult<List<Progress>> progressByPlanId = productionClient.getProgressByPlanId(proPlanPo.getId());
        productionClient.getProgressByPlanId(proPlanPo.getId());
        List<Progress> progresses = progressByPlanId.getData();
        if (progresses.size() != 0){
            map.put("生产进度信息",progresses);
        }
        return map;
    }

}
